Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs.Tiff
Imports Vintasoft.Imaging.Codecs
Imports Vintasoft.Imaging

Namespace Imaging.Codecs.Dialogs
	Public Partial Class TiffEncoderSettingsForm
		Inherits Form

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()

			EditAnnotationSettings = False
			AppendExistingDocumentEnabled = False
		End Sub


		#End Region



		#Region "Properties"

		Private _encoderSettings As TiffEncoderSettings
		''' <summary>
		''' Gets or sets TIFF encoder settings.
		''' </summary>
		Public Property EncoderSettings() As TiffEncoderSettings
			Get
				Return _encoderSettings
			End Get
			Set
				If value Is Nothing Then
					Throw New ArgumentOutOfRangeException()
				End If
				If _encoderSettings IsNot value Then
					_encoderSettings = value
					ShowEncoderSettings()
				End If
			End Set
		End Property

		''' <summary>
		''' Determines that existing document should be append.
		''' </summary>
		Public Property AppendExistingDocument() As Boolean
			Get
				Return appendCheckBox.Checked
			End Get
			Set
				appendCheckBox.Checked = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets a value indicating whether <see cref="AppendExistingDocument"/> is enabled.
		''' </summary>
		Public Property AppendExistingDocumentEnabled() As Boolean
			Get
				Return appendCheckBox.Enabled
			End Get
			Set
				appendCheckBox.Enabled = value
			End Set
		End Property

		Private _formatOfAnnotations As AnnotationsFormat
		Public Property FormatOfAnnotations() As AnnotationsFormat
			Get
				Return _formatOfAnnotations
			End Get
			Set
				_formatOfAnnotations = value
				annotationsBinaryCheckBox.Checked = (_formatOfAnnotations And AnnotationsFormat.VintasoftBinary) <> 0
				annotationXmpCheckBox.Checked = (_formatOfAnnotations And AnnotationsFormat.VintasoftXmp) <> 0
				annotationWangCheckBox.Checked = (_formatOfAnnotations And AnnotationsFormat.Wang) <> 0
			End Set
		End Property

		Public Property EditAnnotationSettings() As Boolean
			Get
				Return tabControl1.TabPages.Contains(annotationsTabPage)
			End Get
			Set
				If EditAnnotationSettings <> value Then
					If value Then
						tabControl1.TabPages.Add(annotationsTabPage)
					Else
						tabControl1.TabPages.Remove(annotationsTabPage)
					End If
				End If
			End Set
		End Property


		#End Region



		#Region "Methods"

		Protected Overrides Sub OnLoad(e As EventArgs)
			MyBase.OnLoad(e)

			If EncoderSettings Is Nothing Then
				EncoderSettings = New TiffEncoderSettings()
			End If
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			SetEncoderSettings()

			DialogResult = DialogResult.OK
		End Sub

		Private Sub ShowEncoderSettings()
			zipLevelNumericUpDown.Value = EncoderSettings.ZipLevel
			jpegGrayscaleCheckBox.Checked = EncoderSettings.SaveJpegAsGrayscale
			jpegQualityNumericUpDown.Value = EncoderSettings.JpegQuality

			Select Case EncoderSettings.Compression
				Case TiffCompression.CcittGroup3, TiffCompression.CcittGroup4
					compressionCcittRadioButton.Checked = True
					Exit Select
				Case TiffCompression.Jpeg
					compressionJpegRadioButton.Checked = True
					Exit Select
				Case TiffCompression.Lzw
					compressionLzwRadioButton.Checked = True
					Exit Select
				Case TiffCompression.None
					compressionNoneRadioButton.Checked = True
					Exit Select
				Case TiffCompression.Zip
					compressionZipRadioButton.Checked = True
					Exit Select
				Case Else
					compressionAutoRadioButton.Checked = True
					Exit Select
			End Select
		End Sub

		Private Sub SetEncoderSettings()
			EncoderSettings.ZipLevel = CInt(Math.Truncate(zipLevelNumericUpDown.Value))
			EncoderSettings.SaveJpegAsGrayscale = jpegGrayscaleCheckBox.Checked
			EncoderSettings.JpegQuality = CInt(Math.Truncate(jpegQualityNumericUpDown.Value))

			If compressionAutoRadioButton.Checked Then
				EncoderSettings.Compression = TiffCompression.Auto
			ElseIf compressionCcittRadioButton.Checked Then
				EncoderSettings.Compression = TiffCompression.CcittGroup4
			ElseIf compressionJpegRadioButton.Checked Then
				EncoderSettings.Compression = TiffCompression.Jpeg
			ElseIf compressionLzwRadioButton.Checked Then
				EncoderSettings.Compression = TiffCompression.Lzw
			ElseIf compressionNoneRadioButton.Checked Then
				EncoderSettings.Compression = TiffCompression.None
			ElseIf compressionZipRadioButton.Checked Then
				EncoderSettings.Compression = TiffCompression.Zip
			End If

			If EditAnnotationSettings Then
				_formatOfAnnotations = AnnotationsFormat.None
				If annotationsBinaryCheckBox.Checked Then
					_formatOfAnnotations = _formatOfAnnotations Or AnnotationsFormat.VintasoftBinary
				End If
				If annotationXmpCheckBox.Checked Then
					_formatOfAnnotations = _formatOfAnnotations Or AnnotationsFormat.VintasoftXmp
				End If
				If annotationWangCheckBox.Checked Then
					_formatOfAnnotations = _formatOfAnnotations Or AnnotationsFormat.Wang
				End If
			End If
		End Sub

		Private Sub compressionZipRadioButton_CheckedChanged(sender As Object, e As EventArgs)
			zipGroupBox.Enabled = compressionZipRadioButton.Checked
		End Sub

		#End Region

	End Class
End Namespace
